﻿package com.google.ads.studio.creative.swipe {
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.display.Loader;
	import flash.display.Shape;
	import flash.display.SimpleButton;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.utils.getQualifiedSuperclassName;
	
	import com.greensock.TweenLite;
	import com.google.ads.studio.innovation.youtube.player.proxy.YTPlayer;
	
	/**
	* MediaWindow Class
	* Content panel for the SwipeBar Class.
	* 
	* @autor: Emilio Mora (emiliom@google.com)
	* @version: 0.2
	* @langversion AS3
	*
	*/	
	public class MediaWindow extends MovieClip {
		
		private var videoPlayerInstance:YTPlayer;
		private var haveVideo:Boolean = false;
		
		public var animatedShow:Boolean = true;
		public var animatedShowTime:Number = 0.5;
		
		/**
		* Constructor.
		*
		* @param image Background image of the panel, can be a MovieClip, BitmapData or a URL to an image.
		* @param videoID Youtube Id for the video to show (optional).
		* @param videoX X Position of the video.
		* @param videoY Y Position of the video.
		* @param videoWidth Width of the video.
		* @param videoHeight Height of the video.
		*/
		public function MediaWindow(image:Object = null, videoID:String = null, videoX:Number = 0, videoY:Number = 0, videoWidth:Number = 0, videoHeight:Number = 0) {
			
			this.visible = false;
			this.alpha = 0;
			
			if (image) addBackgroundImage(image);
			if (videoID) createVideo(videoID, videoX, videoY, videoWidth, videoHeight);
			
			close_btn.addEventListener(MouseEvent.CLICK, onCloseClick);
			close_btn.addEventListener(MouseEvent.MOUSE_OVER, mouseOverEffect);
			close_btn.addEventListener(MouseEvent.MOUSE_OUT, mouseOutEffect);
			
		}
		
		private function mouseOverEffect(event:MouseEvent) {
			TweenLite.to(event.currentTarget, 0.5, {alpha:0.6});
		}
		
		private function mouseOutEffect(event:MouseEvent) {
			TweenLite.to(event.currentTarget, 0.5, {alpha:1});
		}
		
		/**
		* Add a background image to the panel.
		* 
		* @param image Background image of the panel, can be a MovieClip, BitmapData or a URL to an image.
		*/
		private function addBackgroundImage(image:Object) {
			if (getQualifiedSuperclassName(image) == "flash.display::MovieClip") {
				var imageMovie:MovieClip = new image();
				imageLoader_mc.addChild(imageMovie);
			} else if (getQualifiedSuperclassName(image) == "flash.display::BitmapData") {
				var bitmapData:BitmapData = new image(0,0);
				var bitmap:Bitmap = new Bitmap(bitmapData);
				imageLoader_mc.addChild(bitmap);
			} else if (image is String) {
				var imageLoader:Loader = new Loader();
				imageLoader.load( new URLRequest (image as String) );
				imageLoader_mc.addChild(imageLoader);
			} else {
				throw new Error('Invalid panel image, the image need to be a MovieClip, BitmapData or file path');
			}
		}
		
		/**
		* Creates a new YTPlayer component instance.
		*
		* @param videoID Youtube Id for the video to show.
		* @param videoX X Position of the video.
		* @param videoY Y Position of the video.
		* @param videoWidth Width of the video.
		* @param videoHeight Height of the video.
		*/
		private function createVideo(videoID, videoX, videoY, videoWidth, videoHeight) {
			haveVideo = true;
			
			videoPlayerInstance = new YTPlayer();
			videoPlayerInstance.x = videoX;
			videoPlayerInstance.y	 = videoY;
			videoPlayerInstance.width = videoWidth;
			videoPlayerInstance.height = videoHeight;
			videoPlayerInstance.version = 0;
			videoPlayerInstance.proxy.videoId = videoID;
			videoPlayerInstance.proxy.autoHideControls = true;
			
			videoLoader_mc.addChild(videoPlayerInstance);
			
			var videoMask:Shape = new Shape();
			videoMask.x = videoX;
			videoMask.y = videoY;
			videoMask.graphics.beginFill(0xFF0000,1);
			videoMask.graphics.drawRect(0,0,videoWidth,videoHeight);
			videoMask.graphics.endFill();
			
			videoLoader_mc.addChild(videoMask);
			
			videoPlayerInstance.mask = videoMask;
			
			windowHitArea_btn.addEventListener(MouseEvent.CLICK, pauseVideo);
		}
		
		/**
		* Pause the video when the user click in the panel if the panel have a video.
		* 
		* @param event MouseEvent.
		*/
		private function pauseVideo(event:MouseEvent = null) {
			videoPlayerInstance.proxy.pause();
		}
		
		/**
		* Hide the panel when the user click in the close button.
		* 
		* @param event MouseEvent.
		*/
		private function onCloseClick(event:MouseEvent) {
			hide();
		}
		
		/**
		* Show the panel.
		* 
		* @param animated Fade animation on the panel show if true (default: true).
		*/
		public function show(animated:Boolean = true) {
			this.visible = true;
			if (animated && animatedShow) {
				TweenLite.to(this, animatedShowTime, {alpha:1});
			} else {
				this.alpha = 1;
			}
		}
		
		/**
		* Hide the panel.
		* 
		* @param animated Fade animation on the panel hide if true (default: true).
		*/
		public function hide(animated:Boolean = true) {
			if (animated && animatedShow) {
				TweenLite.to(this, animatedShowTime, {alpha:0, onComplete: function (m:MovieClip) {
					m.visible = false;
				}, onCompleteParams:[this]});
			} else {
				this.visible = false;
				this.alpha = 0;
			}
			
			if (haveVideo) pauseVideo();
		}

		/**
		* The hit button of the panel to create mouse event listeners.
		* 
		* @return The button.
		*/
		public function get hitAreaButton():SimpleButton {
			return windowHitArea_btn;
		}
		
		/**
		* The YouTube Video Player instance.
		* 
		* @return The instance.
		*/
		public function get videoPlayer() {
			return videoPlayerInstance;
		}


	}
	
}
